﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Bricks.Business.Service.Impl;
using Bricks.Site.Models;
using Bricks.Site.Models.DBViewModel.Project;
using Bricks.Business.Models.FlowProcess;
using System.Xml;
using System.Web.Script.Serialization;
using Bricks.Business.Service;
using Bricks.Component.Utils;
using Bricks.Site.Web.CommonUtils;
using Bricks.Component.Data;
using Bricks.Component.Utils.Extensions;
using Bricks.Business.Models.ProjectContract;
using Bricks.Site.Models.DBViewModel.Contract;
using Bricks.Site.Models.DBViewModel.Expense;


namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class ReportController : BaseController
    {
        [Import]
        public IProjectInfoService ProjectInfoService { get; set; }
        [Import]
        public ISectionSalaryService SectionSalaryService { get; set; }
        [Import]
        public IStageScheduleService StageScheduleService { get; set; }
        [Import]
        public IYearPlanService YearPlanService { get; set; }
        [Import]
        public IMonthPlanService MonthPlanService { get; set; }
        [Import]
        public IExpenseService ExpenseService { get; set; }
        [Import]
        public IEngineeringImageScheduleService EngineeringImageScheduleService { get; set; }


        public ActionResult WorkersWagesMonitoringReport()
        {
            return View();
        }

        [HttpPost]
        public ActionResult WorkersWagesMonitoringReport(string projectID, string contractType, string contractID, string year, string month)
        {
            Guid guidProjectID = string.IsNullOrEmpty(projectID) ? GuidHelper.NewGuid() : projectID.ToGuid();
            Guid guidContractID = string.IsNullOrEmpty(contractID) ? GuidHelper.NewGuid() : contractID.ToGuid();
            ViewData["ReportData"] = SectionSalaryService.GetSectionSalaryDetailList(guidProjectID, contractType, guidContractID, year, month);

            string projectName = string.Empty;
            ProjectInfo projectInfo = ProjectInfoService.ProjectInfoes.SingleOrDefault(p => p.ID == guidProjectID);
            if (projectInfo != null)
            {
                projectName = projectInfo.ProjectName;
            }

            string contractName = string.Empty;
            string sql = "select * from V_ContractInfoes where ID ='" + contractID + "'";
            var lstContractInfo = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);
            if (lstContractInfo != null && lstContractInfo.Count == 1)
            {
                contractName = lstContractInfo[0].ContractName;
            }

            ViewData["CurrentReportName"] = projectName + "项目" + contractName + "标段" + year + "年" + month + "月农民工工资表";
            return View();
        }

        public ActionResult StageReport()
        {
            return View();
        }

        [HttpPost]
        public ActionResult StageReport(string year, string month, string stage)
        {
            ViewData["ReportData"] = StageScheduleService.GetStageReportView(year, month, stage, "施工合同");
            ViewData["CurrentReportName"] = "收费处公路基本建设工程旬报";
            string stageNum = month + "月1号到" + month + "月10号";
            if (stage == "中旬")
            {
                stageNum = month + "月11号到" + month + "月20号";
            }
            else if (stage == "下旬")
            {
                stageNum = month + "月21号到当月最后一天";
            }
            ViewData["TotalTime"] = year + "年" + stageNum;
            return View();
        }

        public ActionResult TableTwoReport()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TableTwoReport(string projectID, string contractType, string contractID, int? contractStagesID)
        {
            if (contractStagesID != null)
            {
                TableTwoReportTitleView tableTwoReportTitleView = new Models.TableTwoReportTitleView();
                Guid guidProjectID = string.IsNullOrEmpty(projectID) ? GuidHelper.NewGuid() : projectID.ToGuid();
                Guid guidContractID = string.IsNullOrEmpty(contractID) ? GuidHelper.NewGuid() : contractID.ToGuid();
                ProjectInfo projectInfo = ProjectInfoService.ProjectInfoes.SingleOrDefault(p => p.ID == guidProjectID);
                if (projectInfo != null)
                {
                    tableTwoReportTitleView.ProjectName = projectInfo.ProjectName;
                }

                string sql = "select * from V_ContractInfoes where ID ='" + contractID + "'";
                var lstContractInfo = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);
                if (lstContractInfo != null && lstContractInfo.Count == 1)
                {
                    tableTwoReportTitleView.ContractCode = lstContractInfo[0].ContractCode;
                }

                string jzDate = string.Empty;
                sql = "select ID,JSRQ from XMQC where XMID ='" + projectID + "' and BDID='" + contractID + "' and QCID='" + contractStagesID + "'";
                var lstXMQC = DbSqlHelper<XMQCView>.SqlQueryCommon(sql);
                if (lstXMQC != null && lstXMQC.Count == 1)
                {
                    tableTwoReportTitleView.JZDate = lstXMQC[0].JSRQ == null ? string.Empty : Convert.ToDateTime(lstXMQC[0].JSRQ).ToString("yyyy-MM-dd");
                }

                sql = @"select aa.BDMC,bb.JLMC 
                        from(select  1 as F,HTDWMC as BDMC,HTBH as BGHTBH from ZZJGXXEX where id='" + contractID + @"') aa left join 
                        (select 1 as F,HTDWMC as JLMC from ZZJGXXEX where id in (select pid from ZZJGXX where id='" + contractID + @"')) bb on aa.f=bb.f";
                var lstZZJGXX = DbSqlHelper<ZZJGXXView>.SqlQueryCommon(sql);
                if (lstZZJGXX != null && lstZZJGXX.Count == 1)
                {
                    tableTwoReportTitleView.SGDW = lstZZJGXX[0].BDMC;
                    tableTwoReportTitleView.JLDW = lstZZJGXX[0].JLMC;
                }
                tableTwoReportTitleView.ZFBH = ExpenseService.GetExpensePayNum(guidProjectID, guidContractID, contractStagesID);

                List<TableTwoReportTitleView> lstTableTwoReportTitleView = new List<TableTwoReportTitleView>();
                lstTableTwoReportTitleView.Add(tableTwoReportTitleView);
                ViewData["TableTwoReportTitleView"] = lstTableTwoReportTitleView;


                LineView lineView = new LineView();
                string totalReviseMoney = YearPlanService.GetTotalReviseMoney(projectID.ToGuid(), "", contractID.ToGuid());
                List<VMQCYearMonth> lstVMQCYearMonth = ExpenseService.GetVMQCYearMonth(contractID, Convert.ToInt32(contractStagesID));
                if (!string.IsNullOrEmpty(totalReviseMoney))
                {
                    lineView.lstPointView = YearPlanService.GetYearPlanComplete(projectID.ToGuid(), contractID.ToGuid(), Convert.ToDouble(totalReviseMoney));

                    if (lstVMQCYearMonth.Count == 1)
                    {
                        VMQCYearMonth vmQCYearMonth = lstVMQCYearMonth.First();
                        lineView.lstPointViewOne = MonthPlanService.GetMonthPlanComplete(projectID.ToGuid(), contractID.ToGuid(), Convert.ToDouble(totalReviseMoney), vmQCYearMonth.Year, vmQCYearMonth.Month);
                    }
                }
                lineView.lstPointViewTwo = ExpenseService.GetZFComplete(contractID, Convert.ToInt32(contractStagesID));

                List<TableTwoReportChartView> lstTableTwoReportView = new List<TableTwoReportChartView>();
                foreach (PointView item in lineView.lstPointView)
                {
                    lstTableTwoReportView.Add(new TableTwoReportChartView() { X = item.X, Y = item.Y, Type = "计划完成" });
                }
                foreach (PointView item in lineView.lstPointViewOne)
                {
                    lstTableTwoReportView.Add(new TableTwoReportChartView() { X = item.X, Y = item.Y, Type = "实际完成" });
                }
                foreach (PointView item in lineView.lstPointViewTwo)
                {
                    lstTableTwoReportView.Add(new TableTwoReportChartView() { X = item.X, Y = item.Y, Type = "实际支付" });
                }
                ViewData["TableTwoReportChartView"] = lstTableTwoReportView;

                ViewData["TableTwoReportTableView"] = ExpenseService.GetVMExpenseSecondList(contractID, Convert.ToInt32(contractStagesID));
            }
            else
            {
                ViewData["TableTwoReportTitleView"] = null;
                ViewData["TableTwoReportChartView"] = null;
                ViewData["TableTwoReportTableView"] = null;
            }
            return View();
        }

        public ActionResult EngineeringImageScheduleReport()
        {
            return View();
        }

        [HttpPost]
        public ActionResult EngineeringImageScheduleReport(string projectID, string year, string month)
        {
            if (!string.IsNullOrEmpty(projectID))
            {
                EngineeringImageScheduleReportView view = EngineeringImageScheduleService.GetEngineeringImageScheduleListForReport(projectID.ToGuid(), year, month);
                view.Year = (Convert.ToInt32(year) - 1).ToString();
                ViewData["EngineeringImageScheduleReportView"] = view;
            }
            else
            {
                ViewData["EngineeringImageScheduleReportView"] = null;
            }
            return View();
        }
    }
}